package wnhz.com.business.rebateshop;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTabHost;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.R;
import wnhz.com.business.base.MyBaseActivity;
import wnhz.com.business.rebateshop.fragment.CarFragment;
import wnhz.com.business.rebateshop.fragment.HomeFragment;
import wnhz.com.business.rebateshop.fragment.MineFragment;
import wnhz.com.business.rebateshop.fragment.RebateFragment;
import wnhz.com.business.rebateshop.fragment.SortFragment;

/**
 * Created by lizhenglong on 2017-09-18 0018.
 */

public class RebateShopActivity extends MyBaseActivity implements View.OnClickListener {

    @BindView(R.id.main_realtabcontent)
    FrameLayout frameLayout;
    @BindView(R.id.main_tabhost)
    FragmentTabHost fragmentTabHost;//底部按钮

    /**
     * 带状态选择器的图标
     */
    private int[] mTabSelectors = new int[]{R.drawable.bg_rbtn_1, R.drawable.bg_rbtn_2, R.drawable.bg_rbtn_3, R.drawable.bg_rbtn_4, R.drawable.bg_rbtn_5};
    private Class[] mFragments = new Class[]{HomeFragment.class,SortFragment.class, CarFragment.class, RebateFragment.class, MineFragment.class};
    private String[] mTabSpecs = new String[]{"Main", "Second", "Third", "Fourth", "Fifth"};
    private String[] tabname = new String[]{"首页", "分类", "购物车", "返利分销", "我的"};
    private TextView mTvNumInCart;

    @Override
    protected void handleMsg(Message msg) {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_rebate);
        ButterKnife.bind(this);
        fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.main_realtabcontent);
        addTab();

    }

    /**
     * 添加底部按钮
     */
    private void addTab() {
        for (int i = 0; i < mFragments.length; i++) {
            View tabIndicator = getLayoutInflater().inflate(
                    R.layout.rebate_indicator, null);//底部菜单布局
            ImageView imageView = (ImageView) tabIndicator
                    .findViewById(R.id.imageView1);
            imageView.setImageResource(mTabSelectors[i]);
            TextView textView = (TextView) tabIndicator.findViewById(R.id.tv_names);
            textView.setText(tabname[i]);
            if (i == 0) {
                textView.setTextColor(getResources().getColor(R.color.bg_main));
            }
            if (i == 2) {
                mTvNumInCart = (TextView) tabIndicator.findViewById(R.id.textView1);//右上角数量
            }
            fragmentTabHost.addTab(fragmentTabHost.newTabSpec(tabname[i])
                    .setIndicator(tabIndicator), mFragments[i], null);
            tabIndicator.setId(i);
            tabIndicator.setOnClickListener(this);
        }
        //文字选中变化
        fragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {
                for (int i = 0; i < fragmentTabHost.getTabWidget().getChildCount(); i++) {
                    TextView tv = (TextView) fragmentTabHost.getTabWidget().getChildAt(i).findViewById(R.id.tv_names);
                    if (fragmentTabHost.getCurrentTab() == i) {//选中
                        tv.setTextColor(getResources().getColor(R.color.red255));
                    } else {//不选中
                        tv.setTextColor(getResources().getColor(R.color.text51));
                    }
                }

            }
        });

    }

    public void setTab(int tab) {
        fragmentTabHost.setCurrentTab(tab);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case 0:
                fragmentTabHost.setCurrentTabByTag(tabname[0]);
                break;
            case 1:
                fragmentTabHost.setCurrentTabByTag(tabname[1]);
                break;
            case 2:
                fragmentTabHost.setCurrentTabByTag(tabname[2]);
                break;
            case 3:
                fragmentTabHost.setCurrentTabByTag(tabname[3]);
                break;
            case 4:
                fragmentTabHost.setCurrentTabByTag(tabname[4]);
                break;
        }
    }
}
